package dsl.rulesengine

class RulesEngineMetaClass extends DelegatingMetaClass {

    def engine
    def classMethods = []

    RulesEngineMetaClass(Class theClass, RulesEngineDelegator engine) {
        super(theClass)
        theClass.methods.each {
            classMethods << it.name
        }
        this.engine = engine
    }

    public Object invokeMethod(Object obj, String methodName, Object arg) {
//        println("Executed ${s} through RulesEngineMetaClass")
        return delegateMethod(obj, methodName, arg)
    }

    public Object invokeMethod(Object obj, String methodName, Object[] args) {
//        println("Executed ${s} through RulesEngineMetaClass")
        return delegateMethod(obj, methodName, args)
    }


    def delegateMethod(obj, methodName, args) {
        if(classMethods.contains(methodName)) {
            return super.invokeMethod(obj, methodName, args)
        } else {
            return engine.invokeMethod(methodName, args)
        }
    }

}
